package com.cz.mes;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 补偿工艺流程
 */
public class PuChangTechnologicalProcess {

    public static void main(String[] args) {

        try {
            String sql = "select * from production_warehouse where document_time> '2025-03-07 00:00:00' and document_time<'2025-04-02 00:00:00' and state in (0,3,4) ";
            List<Entity> entityList = Db.use("mes-prod-master").query(sql);
            for (Entity entity : entityList) {
               String billNo =  entity.getStr("bill_no");
               String form = entity.getStr("form");
               String manufacture_order_detail_id = entity.getStr("manufacture_order_detail_id");
               if(form.equals("AL")){
                   Map<String,Object> hashMap = new HashMap<>();
                   hashMap.put("inbound_note_no",billNo);
                   hashMap.put("original_note_detail_id",manufacture_order_detail_id);
                   String sql2= "select  original_note_no,original_note_detail_id,inbound_num from wms4db.wms_inbound_note_detail where original_note_no = :inbound_note_no and original_note_detail_id = :original_note_detail_id and status = 1 and note_status = 29";
                   Entity entity1 =Db.use("mes-prod-master").queryOne(sql2, hashMap);
                   if(entity1 != null){
                       System.out.println(billNo + "已入库"+",在mes中没有入库,类型"+form);
                       HashMap<String,Object> releaseMap = new HashMap<>();
                       releaseMap.put("originalNoteNo",entity1.getStr("original_note_no"));
                       releaseMap.put("originalNoteDetailId",entity1.get("original_note_detail_id"));
                       releaseMap.put("inboundNum",entity1.getStr("inbound_num"));
                       System.out.println(billNo+"xxxx"+JSONUtil.toJsonStr(releaseMap));
                       clearAl(releaseMap);
                   }
               }else if(form.equals("FA")){
                   Map<String,Object> hashMap = new HashMap<>();
                   hashMap.put("inbound_note_no",billNo);
                   hashMap.put("original_note_detail_id",manufacture_order_detail_id);
                   String sql2= "select  original_note_no,original_note_detail_id,inbound_num from wms4db.wms_inbound_note_detail where original_note_no = :inbound_note_no and original_note_detail_id = :original_note_detail_id and status = 1 and note_status = 29";
                   Entity entity1 =Db.use("mes-prod-master").queryOne(sql2, hashMap);
                   if(entity1 != null){
                       System.out.println(billNo + "已入库"+",在mes中没有入库,类型"+form);
                       HashMap<String,Object> releaseMap = new HashMap<>();
                       releaseMap.put("originalNoteNo",entity1.getStr("original_note_no"));
                       releaseMap.put("originalNoteDetailId",entity1.get("original_note_detail_id"));
                       releaseMap.put("inboundNum",entity1.getStr("inbound_num"));
                       System.out.println(billNo+"xxxx"+JSONUtil.toJsonStr(releaseMap));
                       clearAl(releaseMap);
                   }
               }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void clearAl(Map<String, Object> releaseMap) {
        String url = "http://gateway.prod.ant2world.com/at-mes-api/technologicalProcess/physicalStorage";
        HttpRequest request = HttpUtil.createPost(url);
        String token ="Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX3R5cGUiOjEsImRlcGFydG1lbnRfaWQiOiJbMTQsIDE0OCwgMjM4XSIsInVzZXJfbmFtZSI6ImFkbWluIiwic2NvcGUiOlsiYWxsIl0sImlkIjoxLCJleHAiOjE3NDM1OTcxMDUsImF1dGhvcml0aWVzIjpbIjQ4X-WVhuWfjuWuouaIt-WuoeaguCIsIjQwX0NNU-euoeeQhiIsIjExN1_lupPkvY3kvZPnp6_nrqHnkIbliJfooagiLCI0MV9NRVMtRkEiLCIyNTJfQk0wMDAwNjkiLCIyNTZfQk0wMDAzMzAiLCI2Ml9XTVMtSVFD57uE6ZW_IiwiMTAwX-WVhuWfjuebruW9leS5pue7tOaKpCIsIjQ5X-S4gOWuouS4gOS7tyIsIjIyM19tZXMtZWZmaWNpZW5jeS1sZWFkZXIiLCI0NF9NRVMt6LSo5qOA5ZGYIiwiMjMwX2N6LWFwcHJvdmUiLCIzMV9wbXMt5ZWG5ZOB566h55CGIiwiMjQxX-aWsHdtc-adg-mZkC3plIDllK7lhoXli6TkuLvnrqHlupPlrZjnrqHnkIYiLCIyMDhfbWVzLW91dH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SSRNnrH9uS9Ly9PbSgE3Blse7UEmvNWiqCBiAKSj6x08PbILN3lBtmkbcKjH2DmRvUaJSjv5AZnjac3p7VI2r7CrHecSgA";
        request.header("Authorization", token);
        String data = JSONUtil.toJsonStr(releaseMap);
        System.out.println("请求参数:" + data);
        request.body(data, "application/json");
        HttpResponse response = request.execute();
        if (response.isOk()) {
            System.out.println(response.body());
        } else {
            System.out.println("error:" + response.getStatus());
        }
    }
}
